import axios from 'axios'
import qs from 'qs'
import store from '../store/store'

const icbcHttp = axios.create({
  baseURL: 'https://apipcs3.dccnet.com.cn',
  // baseURL: 'https://gw.open.icbc.com.cn',
  timeout: 30000
})
// 拦截器  请求之前做点啥
icbcHttp.interceptors.request.use(
  config => {
    // 判断store是否有token 有就加入请求头
    const token = store.state.token
    if (token) {
      config.headers.token = token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
// 返回的时候拦截一下
icbcHttp.interceptors.response.use(
  res => {
    if (res.data.code == 200) {
      res.data.status = 1
    } else {
      res.data.status = 0
    }
    return res.data
  },
  error => {
    // 异常返回 根据相应的状态码进行对应操作
    if (error.response) {
      error.response.data.status = 0
      switch (error.response.status) {
        case 403:
          store.dispatch('userLogout') // 可能是token过期，清除它
          window.location.href = '/'
          break
        case 401:
          store.dispatch('userLogout') // 可能是token过期，清除它
          window.location.href = '/'
          break
        default:
          return error.response.data
      }
    }
  }
)
export const icbcEaccount = (params: object) => {
  return icbcHttp.post('/ui/eaccount/manage/V1', qs.stringify(params))
}

export const settlementAccount = (params: object) => {
  return icbcHttp.post('/api/settlement/account/open/V2', qs.stringify(params))
}
